package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.ExamineeDao;
import com.admission.model.ArrayInfo;
import com.admission.model.Examinee;
import com.admission.util.DeleteFile;

@Repository
@Transactional
public class ExamineeManager {
	private ExamineeDao examineeDao;
	private ArrayInfoManager arrayInfoManager;
	
	public void save(Examinee entity,String photoUrl) throws Exception{
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		if(photoUrl!=null){
			File file = new File(serverrealPath + File.separator
					+ entity.getPhotoUrl());
			DeleteFile.deleteByFile(file);// 删除源文件	
		 entity.setPhotoUrl(photoUrl);
		}
		examineeDao.save(entity);
	}
	
	
	/**
	 * 改变状态
	 * 
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status) {
		arrayInfoManager.changeConsult(status, ArrayInfo.SPORTCONSULT);
	}

	/**
	 * 判断权限
	 * 
	 * @return
	 */
	public String checkConsult() {
		return arrayInfoManager.checkConsult(ArrayInfo.SPORTCONSULT);
	}
	
	public void save(Examinee entity){
		examineeDao.save(entity);
	}
	
	public void deleteById(String id){
		examineeDao.delete(id);
	}
	
	public void deleteExamineeByIds(String ids){
		for(String id : ids.split(",")){
			examineeDao.delete(id.trim());
		}
	}
	
	public List<Examinee> list(){
		return examineeDao.getAll();
	}
	
	public Examinee getById(String id){
		return examineeDao.get(id);
	}
	
	public Examinee getByIdentity(String identity){
		return examineeDao.findUniqueBy("identity", identity);
	}
	
	@Transactional(readOnly = true)
	public Page<Examinee> pageExaminee(final Page<Examinee> page, final List<PropertyFilter> filters){
		return examineeDao.findPage(page, filters);
	}

	@Autowired
	public void setExamineeDao(ExamineeDao examineeDao) {
		this.examineeDao = examineeDao;
	}
	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}
}
